<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="菜单名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="selectmenus">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <el-table :data="menus" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="150">
      </el-table-column>
      <el-table-column prop="component" label="组件" width="150">
      </el-table-column>

      <el-table-column prop="url" label="地址" width="100">
      </el-table-column>
      <el-table-column prop="icon" label="图标" width="150">
      </el-table-column>
      <el-table-column prop="index" label="排序" width="150">
      </el-table-column>
      <el-table-column prop="intro" label="标识" width="150">
      </el-table-column>
      <el-table-column label="状态" width="150" sortable>
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.state==1">启用</span>
          <span style="color: red" v-else>禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
		</el-col>
		<!--新增界面-->
    <!-- 添加或修改的对话框
    el-dialog - elementUi提供的对话框 = 模态框
      :title="title" - 动态改变标题
      :visible.sync="saveFormVisible"  - 模态框的显示状态
      :close-on-click-modal="false" - 关闭模态框只能通过模态框的x关闭，点击空白处不能关闭模态框
    el-form - 表单
      :model="saveForm" - 绑定表单对象
      label-width="80px" - 标签宽度
      :rules="saveFormRules" - 验证规则
      ref="saveForm" - 为当前模态框取名
      prop="name" - 如果当前字段要验证，就必须写，出现*
      auto-complete="off" - 不自动提示
-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="上级菜单">
          <el-cascader
                v-model="saveForm.parentId"
               :options="options"
               :props="{
                checkStrictly: true,
                label:'name' ,
                value:'id'
              }"
                clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="index">
          <el-input-number v-model="saveForm.index" controls-position="right" @change="handleChange" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="访问URL" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件路径" prop="component">
          <el-input v-model="saveForm.component" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="标识" prop="intro">
          <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-model="saveForm.state" label="true">正常</el-radio>
          <el-radio v-model="saveForm.state" label="false">禁用</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交	</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				menus: [],
        Emps:[],
        options:[],
				totals: 0,
        currentPage: 1,
        pageSize:5,
        title:'',
				listLoading: false,
				sels: [],//列表选中列

        saveFormVisible: false,//编辑界面是否显示
        saveLoading: false,
        saveFormRules: {
					/*name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]*/
				},
				//编辑界面数据
        saveForm: {
          component: '',
					name: '',
          state: '',
          icon:'',
          index: '',
          intro: '',
          parentId: ''
				},
			}
		},
		methods: {
      //排序
      handleChange(value){
       this.saveForm.index=value;
      },
      selectmenus(){
        this.currentPage=1;
        this.getMenu();
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getMenu();
      },
      getMenuTree(){
        this.$http.get("/menu/menusTree").then((res) => {
          if(res.data.success){
            this.options = res.data.resultObj;
            this.listLoading = false;
          }
        }).catch((res=>{
          this.$message.error("网络繁忙");
        }));
      },
			//获取菜单列表
      getMenu() {
				let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.name
				};
				this.listLoading = true;
				this.$http.post("/menu/pageList",para).then((res) => {
            this.menus = res.data.data;
            this.totals = res.data.totals;
            this.listLoading = false;
				}).catch((res=>{
          this.$message.error("网络繁忙");
        }));
			},
			//删除
			handleDel: function (row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/menu/"+row.id).then((res) => {
						this.listLoading = false;
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getMenu();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (row) {
				this.saveFormVisible = true;
        this.title='编辑';
				this.saveForm = Object.assign({}, row);
        console.log(this.saveForm)
/*        //拿到dirpath
        var dirpath= this.saveForm.dirPath.split("/");
        //拿到首尾
        dirpath=dirpath.slice(1,dirpath.length-1);
        for (let i = 0; i < dirpath.length; i++) {
          dirpath[i]=Number(dirpath[i])
        }
        this.saveForm.parentId=dirpath*/
			},
			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
        this.title='新增';
				this.saveForm = {
          component: '',
          name: '',
          state: 'true',
          icon:'',
          index: '',
          intro: '',
          parentId: ''
				};
			},
			//新增
      saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							let para = Object.assign({}, this.saveForm);
              //前端无限级树的处理，有五种情况：null,[],[2],[2,5],2 - 后端用Long接收的不能是数组
              let temp = para.parentId;
              if(temp == null || temp.length==0){
                para.parentId = null;
              } else if(temp.length){//有值，没有报错，数组 [2][2,5]
                para.parentId = temp[temp.length-1];
              }
							this.$http.put("/menu",para).then((res) => {
                if(res.data.success){
                  this.saveLoading = false;
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  this.$refs['saveForm'].resetFields();
                  this.saveFormVisible = false;
                  this.getMenu();
                }else {
                  this.$message.error(res.data.message);
                }
							}).catch(res=>{
                this.$message.error("请求失败");
              });
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          console.log(ids)
					//let para = ids[] ;
					this.$http.patch("/menu",ids).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getMenu();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getMenu();
      this.getMenuTree();
		}
	}

</script>

<style scoped>

</style>